package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;
import java.util.function.Consumer;

/**
 * company: www.abc.com
 * Author: 29746
 * Create Data: 2020/11/10
 */
@Service //
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {
    @Override
    public boolean deleteById(Long id) {
        CheckItem checkItem = baseMapper.selectById(id);
        System.out.println(checkItem);
        checkItem.setIs_delete(1);
        return this.updateById(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        List<CheckItem> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<CheckItem> checkItemPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<CheckItem>().eq("is_delete",0);
        if (queryPageBean.getQueryString()!=null&&"".equals(queryPageBean.getQueryString().trim())==false){
            queryWrapper.and(wrapper -> {
                wrapper.like("code", queryPageBean.getQueryString())
                        .or().like("name", queryPageBean.getQueryString());
            });
        }
        IPage<CheckItem> page = this.page(checkItemPage,queryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public List<CheckItem> findCheckItemByGroupId(Long id) {
        return this.baseMapper.findCheckItemByGroupId(id);
    }
}
